package listbyorder.access001_100.test48;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/1 8:21
 */
public class Solution1 {

    // 旋转图像
    // 循环迭代，一层一层向里面递归
    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length < 2) return;
        int row = matrix.length;
        helper(matrix, 0, 0, row - 1, row - 1);
    }

    private void helper(int[][] matrix, int sa, int sb, int ea, int eb) {
        if (sa >= ea) return;
        // 这里切记，要少算一位
        for (int i = 0; i < eb - sb; i++) {
            int temp = matrix[sa][i + sb];
            matrix[sa][i + sb] = matrix[ea - i][sb];
            matrix[ea - i][sb] = matrix[ea][eb - i];
            matrix[ea][eb - i] = matrix[sa + i][eb];
            matrix[sa + i][eb] = temp;
        }
        helper(matrix, sa + 1, sb + 1, ea - 1, eb - 1);
    }

}
